<?php
namespace app\hotback\controller;
use think\Db;
class Balance extends Base
{
	public function _initialize()
	{
		// 调用父类前置方法
		parent::_initialize();

		// 登录校验
		$this->IsLogin();
	}

	/**
	 * 服务列表
	 */
	public function index()
	{
		$result['code'] = -1;
		$result['msg'] = "非法请求";
		if(request()->isPost())
		{
		    
			$params = input();
			if(!empty($params['page']) && !empty($params['limit']))
			{
			    
			    $map['balance_type']=['in','3,4'];
			    $map['identity_type']='user';
			    $map['status']=1;
			    
				if(!empty($params['title']))
				{
					$map['real_name'] = ['like','%'.$params['title'].'%'];
				}
				
				if(!empty($params['nickname']))
				{
					$map['nickname'] = ['like','%'.$params['nickname'].'%'];
				}
				
				if(!empty($params['mobile']))
				{
				    $uid=db::name('user')->where('mobile',$params['mobile'])->value('id');
					$map['identity_id'] = $uid;
				}
				if(!empty($params['khtype'])){
				    $map['khtype']=$params['khtype'];
				}
				
				$count = Db::name('balance')->where($map)->count();
				$list_data = Db::name('balance')->where($map)->order('id desc')->limit(($params['page']-1)*$params['limit'],$params['limit'])->select();
				if(!empty($list_data))
				{
					//遍历处理
					foreach($list_data as $key => $value)
					{
						$list_data[$key]['nickname']=db::name('user')->where('id',$value['identity_id'])->value('nickname');
						$list_data[$key]['mobile']=db::name('user')->where('id',$value['identity_id'])->value('mobile');
						
						$order=db::name('order')->where('id',$value['order_id'])->find();
    			        $titles=db::name('order_detail')->where('order_id',$value['order_id'])->column('title');
    			        $titleall=implode(',',$titles);
    			        
    			        $list_data[$key]['title']=$titleall.'(￥'.$value['price'].')';
    			        if($value['balance_type']==3){
    			            $beizhu=db::name('user')->where('id',$value['identity_id'])->value('beizhu');
    			            if(!empty($beizhu)){
    			                $list_data[$key]['nickname']=$list_data[$key]['nickname'].'('.$beizhu.')';
    			            }
    			            $list_data[$key]['msg']=db::name('user')->where('id',$order['user_id'])->value('nickname');
    			        }else{
    			            $list_data[$key]['msg']=db::name('techn')->where('user_id',$order['user_id'])->value('nickname');
    			        }
    			        
    			        $list_data[$key]['fwprice']='+'.$value['yes_money'];
    			        $list_data[$key]['order_no']=$order['order_no'];
					}
					$result['code'] = 0;
	        		$result['msg'] = "请求成功";
	        		$result['data'] = $list_data;
	        		$result['count'] = $count;
				}else{
					$result['code'] = -1;
	        		$result['msg'] = "暂无数据";
				}
			}
			echo json_encode($result);
		}else
		{
			return $this->fetch();
		}
	}


}